package com.googlecode.gwt.commons.logging.server;

import java.util.List;

import org.apache.commons.logging.ILogLevel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.LogService;
import org.apache.commons.logging.impl.LogMessage;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class LogServerService extends RemoteServiceServlet implements LogService, ILogLevel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4708601744418650310L;

	private static Log logger = LogFactory.getLog(LogServerService.class);

	/**
	 * Write client log on server logger
	 * @param List logs - log receive from client 
	 */
	public void sendLog(List logs) {
		for (int i = 0; i < logs.size(); i++) {
			LogMessage logMsg = (LogMessage) logs.get(i);
			
			logger.error("Log client level<"+getLevelName(logMsg)+"> class<"+logMsg.getClassName()+"> method<"+ logMsg.getMethod() + "> message<" + logMsg.getMsg()+">", logMsg.getException());
		}
	}
	
	
	private String getLevelName(LogMessage logMsg){
		if (logMsg.getLevel().equals(OFF)) {
			return "OFF";
		} else if (logMsg.getLevel().getIntValue() == SEVERE.getIntValue()) {
			return "SEVERE";
		} else if (logMsg.getLevel().getIntValue() == WARNING.getIntValue()) {
			return "WARNING";
		} else if (logMsg.getLevel().getIntValue() == INFO.getIntValue()) {
			return "INFO";
		} else if (logMsg.getLevel().getIntValue() == ALL.getIntValue()) {
			return "ALL";
		} else if (logMsg.getLevel().getIntValue() == FINE.getIntValue()) {
			return "FINE";
		} else if (logMsg.getLevel().getIntValue() == FINEST.getIntValue()) {
			return "FINEST";
		} else {
			return "UNKNOWN";
		}
	}


	
}
